package blackhouse.thread;

public class ThreadDemo1
{
	public static void main(String[] args)
	{
		TestThread tt = new TestThread();
		new Thread(tt).start();
				try
				{
					Thread.sleep(1);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
		tt.str = "method";
		new Thread(tt).start();
		// new Thread(tt).start();
		// new Thread(tt).start();
	}
}

class TestThread implements Runnable
{
	int tickets = 100;
	String str = new String("");

	@Override
	public void run()
	{

		if (str.equals("method"))
		{
			while (true)
			{
				sale();
			}
		} else
		{
			while (true)
			{
				synchronized (str)
				{
					if (tickets > 0)
					{
						try
						{
							Thread.sleep(10);
						} catch (InterruptedException e)
						{
							e.printStackTrace();
						}
						synchronized(this){}
						System.out.println(Thread.currentThread().getName()
								+ "  is saling ticket" + tickets--);
					}
				}
			}
		}

	}

	public synchronized void sale()
	{
		if (tickets > 0)
		{
			try
			{
				Thread.sleep(10);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			synchronized(str){}
			System.out.print("sale()");
			System.out.println(Thread.currentThread().getName()+ "  is saling ticket" + tickets--);
		}
	}
}
